package net.lainiao.mycost.controller;

import com.sun.org.apache.xalan.internal.xsltc.dom.LoadDocument;
import jxl.read.biff.BiffException;
import net.lainiao.mycost.bean.SalaryPayment;
import net.lainiao.mycost.bean.UserInfo;
import net.lainiao.mycost.dao.IUserInfoDAO;
import net.lainiao.mycost.utils.Comm;
import net.lainiao.mycost.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.invoke.SerializedLambda;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/salary/")
public class SalaryController extends Page {
    @RequestMapping("loadadd.do")
    public String loadadd(Model model) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserMark(Comm.MARK_YES);
        userInfo.setStart(-1);
        List<UserInfo> userInfos = userInfoService.list(userInfo);
        model.addAttribute("userList", userInfos);
        return "salary/salary_add";
    }

    @RequestMapping("add.do")
    public String add(SalaryPayment salaryPayment, Model model, HttpSession session) {
        salaryPaymentService.add(salaryPayment);
        model.addAttribute("errorinfo", "薪资发放成功");
        return loadadd(model);
    }

    @RequestMapping("list.do")
    public String list(SalaryPayment salaryPayment, HttpServletRequest request, Model model) {
        if (salaryPayment == null) {
            salaryPayment = new SalaryPayment();
        }
        this.initPage(request);
        salaryPayment.setStart(this.getPageNo());
        salaryPayment.setLength(PAGE_NUM_BIG);
        model.addAttribute("list", salaryPaymentService.list(salaryPayment));
        model.addAttribute("total", salaryPaymentService.getCount(salaryPayment));
        model.addAttribute("salaryPayment", salaryPayment);
        return "salary/salary_list";
    }

    @RequestMapping("impexecl.do")
    public String impExecl(@RequestParam(value="file",required = false)MultipartFile file,HttpServletRequest request,Model model){
        try {
            salaryPaymentService.addAll(file.getInputStream());
            model.addAttribute("errorinfo","导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("errorinfo","导入失败");
        }
        return list(null,request,model);
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder bin){
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
        bin.registerCustomEditor(Date.class,new CustomDateEditor(format,true));
    }
}
